﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Drawing.Printing;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;

namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SerialPort serialport = new SerialPort("com3");

        private void Form1_Load(object sender, EventArgs e)
        {
            //foreach (string vPortName in SerialPort.GetPortNames())
            //{
            //    this.Text = this.Text + vPortName;
            //}
            serialport.Open();
            if (serialport.IsOpen == true)
            {
                textBox1.Text = serialport.ReadLine().Replace("wn", "").Replace("kg", "");


            }
            else
            {
                MessageBox.Show("端口未打开！");
                return;
            }
            serialport.Close();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //获取天气和解析  
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.weather.com.cn/data/sk/101010200.html");
            request.Timeout = 5000;
            request.Method = "GET";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream());
            string jsonstr = sr.ReadLine();
            JavaScriptSerializer j = new JavaScriptSerializer();
            Weather weathe = new Weather();
            weathe = j.Deserialize<Weather>(jsonstr);
            textBox2.Text = weathe.weatherinfo.city;

            TSCLIB_DLL.openport("Gprinter 2120TF(标签)");
            TSCLIB_DLL.setup("40", "30", "4", "8", "0", "1", "0");
            TSCLIB_DLL.clearbuffer();
            //TSCLIB_DLL.printerfont("20", "0", "TSS24.BF2", "0", "1", "1", "TEST 中文");
            TSCLIB_DLL.windowsfont(20, 0, 44, 0, 2, 0, "ARIAL", "TEST 中文1");
            string test1= "商品重量：" + float.Parse(textBox1.Text) + "千克";
            string test2 = "城市：" + textBox2.Text;
            TSCLIB_DLL.windowsfont(20, 50, 24, 0, 0, 0, "ARIAL", test1);
            TSCLIB_DLL.windowsfont(20, 70, 24, 0, 0, 0, "ARIAL",test2);
            //TSCLIB_DLL.barcode("20", "20", "128", "100", "1", "0", "2", "2", label2.Text);
            //TSCLIB_DLL.windowsfont(20, 150, 24, 0, 0, 0, );
            TSCLIB_DLL.printlabel("1", "1");
            TSCLIB_DLL.closeport();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //获取天气和解析  
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.weather.com.cn/data/sk/101010200.html");
            request.Timeout = 5000;
            request.Method = "GET";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream());
            string jsonstr = sr.ReadLine();
            textBox3.Text = jsonstr;
        }

 

        private void button1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
                button1.Focus();
        }

        private void comboBox1_Click(object sender, EventArgs e)
        {
            //检查是否含有串口  
            string[] str = SerialPort.GetPortNames();
            if (str == null)
            {
                MessageBox.Show("本机没有串口！", "Error");
                return;
            }

            //添加串口项目  
            foreach (string s in System.IO.Ports.SerialPort.GetPortNames())
            {//获取有多少个COM口  
                cbSerial.Items.Add(s);
            }

            //串口设置默认选择项  
            cbSerial.SelectedIndex = 0;         //设置<span style="font-size:18px; "><strong>cbSerial的默认选项</strong></span>  
        }
    }
}
